﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace TxJsq
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string pan = null;
        private static Point rightPosition;
        private void Form1_Shown(object sender, EventArgs e)
        {
            rightPosition = new Point(this.Location.X + 305, this.Location.Y - 92);
            if (File.Exists(@"D:\TxJsq\QMProxyAcceler.exe"))
                setPan("D");
            else if (File.Exists(@"E:\TxJsq\QMProxyAcceler.exe"))
                setPan("E");
            else if (File.Exists(@"F:\TxJsq\QMProxyAcceler.exe"))
                setPan("F");
            else if (File.Exists(@"G:\TxJsq\QMProxyAcceler.exe"))
                setPan("G");
            else
                setPan("D");
            Crack.DisUpdate(pan);
            if (File.Exists(getJxq()))
            {
                Crack.StartJsq(getJxq());
                this.Location = rightPosition;
                this.timer1.Enabled = true;
            }
        }
        private string getJxq()
        {
            return pan + @":\TxJsq\QMProxyAcceler.exe";
        }
        private void msg(string msg)
        {
            MessageBox.Show(this, msg, "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void setPan(string pan) {
            this.pan = pan;
            this.textBox1.Text = this.pan + @":\TxJsq";
        }
        private void textBox1_Leave(object sender, EventArgs e)
        {
            string pan = this.textBox1.Text.ToUpper().Substring(0, 1);
            if ("DEFG".Contains(pan))
                setPan(pan);
            else
                setPan("D");
            Crack.DisUpdate(pan);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(pan))
            {
                msg("目录选择错误！");
                return;
            }
            if (!File.Exists(getJxq()))
            {
                this.button1.Enabled = false;
                Crack.UnPack(pan);
                this.button1.Enabled = true;
            }
            msg("已解压到 " + this.textBox1.Text + " ！");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            msg(Crack.DisUpdate(pan));
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (!File.Exists(getJxq()))
            {
                msg("没找到文件，请先执行第一步解压！");
                return;
            }
            Crack.DisUpdate(pan);
            Crack.StartJsq(getJxq());
            this.Location = rightPosition;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Process.Start("https://gitee.com/liuzy1988/TxJsq");
        }
        private void button5_Click(object sender, EventArgs e)
        {
            msg(Crack.Injection());
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            Crack.KillUpdate();
            //Crack.Injection();
        }
    }
}
